<template>
  <el-container>
    <el-main>
      <el-form :model="form"
               ref="form"
               :rules="rule"
               label-position="right"
               label-width="150px">
        <el-form-item label="轮播图:"
                      prop="imageUrl">
          <el-upload class="avatar-uploader"
                     :action="upload_url"
                     :show-file-list="false"
                     :on-success="uploadSuccess"
                     :before-upload="beforeUpload"
                     :headers="header"
                     name="image">
            <img v-if="form.imageUrl"
                 :src="form.imageUrl"
                 class="avatar" />
            <i v-else
               class="el-icon-plus avatar-uploader-icon"></i>
            <div class="el-upload__tip"
                 slot="tip">图片大小不能超过2M</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="跳转类型:"
                      prop="position_id">
          <el-select v-model.number="form.redirect_type"
                     placeholder="请选择">
            <el-option v-for="row in redirect_type"
                       :key="row"
                       :value="row"
                       :label="row"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跳转链接:"
                      prop="url">
          <el-input type="text"
                    v-model="form.url"
                    style="width:40%;"
                    placeholder="请输入跳转链接"></el-input>
        </el-form-item>
        <el-form-item label="上线时间:"
                      prop="start_date">
          <el-date-picker v-model="form.start_date"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下线时间:"
                      prop="end_date">
          <el-date-picker v-model="form.end_date"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="排序:"
                      prop="sort">
          <el-input type="number"
                    v-model.number="form.sort"
                    min="0"
                    style="width: 40%;"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="success"
                     icon="el-icon-plus"
                     @click.prevent="addBanner"
                     :loading="loading">添加</el-button>
          <el-button type="danger"
                     icon="el-icon-refresh"
                     @click.prevent="reset('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
import { bannerType, addBanner } from '@/api/modules/appConfig'
import { STORAGE } from '@/components/common/global'
export default {
  name: "add",
  data () {
    return {
      upload_url: process.env.VUE_APP_API_BASE_URL + '/api/tool/upload/image',
      loading: false,
      form: {
        imageUrl: '',
        redirect_type: '',
        url: '',
        end_date: '',
        sort: 0,
        start_date: ''
      },
      //轮播图位置列表
      position: [],
      redirect_type: [
        'webview', 'native'
      ],
      rule: {
        imageUrl: [
          { required: true, message: '请上传图片信息', trigger: ['change', 'blur'] }
        ],
        redirect_type: [
          { required: true, message: '跳转类型必选', trigger: ['change', 'blur'] },
        ],
        start_date: [
          { required: true, message: '上线时间必选', trigger: 'blur' },
        ],
        end_date: [
          { required: true, message: '下线时间必选', trigger: 'blur' },
        ],
        sort: [
          { required: true, message: '排序权重必选', trigger: 'blur' },
        ]
      },
      header: {
        'api-token': localStorage.getItem(STORAGE.API_TOKEN)
      },
    };
  },
  methods: {
    uploadSuccess (response, file, fileList) {
      if (response.status == 'success') {
        this.form.imageUrl = response.data;
      } else {
        this.$notify.error(response.failedMsg);
      }
    },
    beforeUpload (file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
    reset (ref) {
      this.$refs[ref].resetFields();
    },
    addBanner () {
      this.$refs.form.validate(res => {
        if (!res) {
          return false;
        }
        var data = {
          images: this.form.imageUrl,
          redirect_type: this.form.redirect_type,
          uptime: this.form.start_date,
          downtime: this.form.end_date,
          sort: this.form.sort,
          position: this.position
        };

        if (this.form.url.trim().length != 0 && this.form.url.trim() != '') {
          data.url = this.form.url;
        }
        this.loading = true;
        addBanner(data).then(response => {
          this.loading = false;
          this.$message.success('添加成功');
          //   this.$message.success(response.data.msg);
          //   this.$router.push({ path: '/appConfig/banner' });
        }).catch(err => {
          this.loading = false;
        })
      });
    }
  },
  mounted () {
    this.position = this.$route.query.position;
    // bannerType().then(response => {
    //   this.position = this.position.concat(response.data);
    // })
  }
}
</script>

<style scoped>
.el-container {
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  border: 1px solid #f2f6fc;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  margin: 10px auto;
  background-color: #fff;
  padding: 20px;
}

.el-header h3 {
  height: 30px;
  text-align: center;
  font-size: 20px;
  line-height: 30px;
  color: gray;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 375px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 100%;
  height: 100%;
}
.el-upload {
  width: auto;
}
</style>